<template>
  <entity-view
    :operator-type="operatorType"
    :model="beanInfo"
    @submit="submit"
    :rules="rules"
    ref="form"
  >
    <div class="sub-content-flex">
      <form-item
        label="商户等级代码"
        v-model="beanInfo.mchntLevelCode"
        prop="mchntLevelCode"
        v-if="operatorType != 'add'"
        :disabled="true"
      ></form-item>
      <form-item
        label="商户等级名称"
        v-model="beanInfo.mchntLevelName"
        prop="mchntLevelName"
      ></form-item>
      <form-item
        label="商户等级描述"
        v-model="beanInfo.mchntLevelDesc"
        prop="mchntLevelDesc"
      ></form-item>
      <form-item
        label="注销原因"
        v-model="beanInfo.cancelReason"
        prop="cancelReason"
        inputType="textarea"
        :valid="operatorType==='cancel'"
        v-if="operatorType==='cancel' || operatorType==='view'"
      ></form-item>
    </div>
  </entity-view>
</template>
<script>
import EntityView from "../../../../components/common/entityView/EntityView.vue";
import FormItem from "../../../../components/common/entityView/FormItem.vue";

export default {
  name: "MchntLevelOperation",
  components: {
    EntityView,
    FormItem,
  },
  props: {
    query: Object,
  },
  data: function () {
    return {
      instId: "",
      modalTitle: "",
      operatorType: "add",
      modalVisible: true,
      selectedRowKeys: [],
      beanInfo: {
        mchntLevelCode: "",
        mchntLevelName: "",
        mchntLevelDesc: "",
      },
      rules: {
        mchntLevelCode: [{ required: true, message: "必输", trigger: "blur" }],
        mchntLevelName: [{ required: true, message: "必输", trigger: "blur" }],
        cancelReason: [{ required: true, message: "必输", trigger: "blur" }],
      },
      loading: false,
    };
  },

  methods: {
    async submit() {
      let res;
      switch (this.operatorType) {
        case "add":
          res = await this.edit();
          break;
        case "edit":
          res = await this.edit();
          break;

        case "view":
          res = true;
          break;
        case "cancel":
          res = await this.edit();
          break;
        default:
          break;
      }
      return res;
    },
    async edit() {
      try {
        const res = await this.$refs.form.validate();
        if (res) {
          let optFlag = {add: `add`, edit: `upd`, view: `view`, use: `use`, stop: `stop`, cancel: `cancel`,chk: `chk`}[this.operatorType]
          const params = {
            ...this.beanInfo,
            instId: this.instId,
            optFlag: optFlag,
          };
          const res = await this.$post(
            "/admin/mcmp/bMchntLevelInfo/edit",
            params
          );
          if (res.data.code === "100000") {
            this.$message.success(res.data.message);
            return true;
          } else {
            this.$message.error(res.data.message);
            return false;
          }
        }
      } catch (e) {
        console.error(e);
      }
    },
    resetFields(instId) {
      this.instId = instId;
      return this.$refs.form && this.$refs.form.resetFields();
    },
    setOperatorType(type) {
      this.operatorType = type;
    },
    setBeanInfo(beanInfo) {
      this.beanInfo = beanInfo;
      this.getOne();
    },
    async getOne() {
      try {
        const res = await this.$get("/admin/mcmp/bMchntLevelInfo/edit", {
          id: this.beanInfo.id,
          instId: this.beanInfo.instId,
          optFlag: "detail",
        });
        this.beanInfo = res.data.entity || {};
      } catch (e) {
        console.error(e);
      }
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../../../../static/less/Common";
.subtitle {
  width: 100%;
  font-size: 18px;
  margin-bottom: 10px;
}
.sub-content-flex {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
}
.sub-content-block {
  display: block;
  flex-wrap: wrap;
  width: 100%;
}
</style>
